jQuery(function(){
	//获取选中状态
	 $(function(){
		  $(":radio").click(function(){
			  serverData.key = jQuery("#keyword").val();
			  var targetType=$('input:radio[name="isReceived"]:checked').val();
			  serverData.targetType = targetType;
			  serverTable.draw();
		  });
	});
	 var targetType=$('input:radio[name="isReceived"]:checked').val();
	 var serverData = {};
	 serverData.key='';
	 serverData.targetType = targetType;
	 serverData.state = '';
	    var serverTable;
	    jQuery(document).ready(function () {
	    	serverTable = jQuery('#dataTable').DataTable({
		        "serverSide": true,
		        "ordering": false,
		        "ajax": {
		            "dataType": "json",
		            "type": "get",
		            "cache": false,
		            "url":window.rootPath+ '/ui/chanceReceive/getpage',
		            "data": function (d) {
		                return $.extend({}, d, serverData);
		            }
		        },
		        "iDisplayLength": 5,
		        columns: [
		            {
		            data: null
		            },
		            {
		                data: null,
		                defaultContent: "",
		                render: function (data, type, full, meta) {
		                	var closeReason = data.saleChance.closeReason;
		                	var operate ='<input type="checkbox" class="checkchild">';
		                    return operate;

		                }
		            },
//		            {
//		                data: null,
//		                defaultContent: "",
//		                render: function (data, type, full, meta) {
//		                	var closeReason = data.saleChance.closeReason;
//		                	var dOut;
//		                	if(closeReason != null){
//		                		dOut ='<p><span class="badge" style="background-color:red;font-family:arial;color:bule">已关闭</span></p>';
//		                	}else{
//		                		
//		                	}
//		                    
//		                    return dOut;
//
//		                }
//		            },
		            {
		            	data: 'saleChance.name',
						render:function(data,type,full,meta){
							if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 20) ? '<font title="'+data+'">'+data.substring(0,20)+'...'+'</font>' : data;
							}
							//return "<div title='"+data+"' style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>"+data+"</div>"
						}
		            },
		            {
		            	data: 'saleChance.chancePriority'
		            },
		            {
		            	//data: 'targetType'
		            	data: null,
		                defaultContent: "",
		                render: function (data, type, full, meta) {
		                	var closeReason = data.saleChance.closeReason;
		                	var chanceType = data.targetType;
		                	var receiveSuperior=data.receiveSuperior;
		                	
		                	var dOut;
		                	if(chanceType == "0" ){
		                		dOut ='<p><span class="badge" style="background-color:red;font-family:arial;color:bule">本部信息</span></p>';
		                	}
		                	else if(chanceType == "1"){
		                		dOut ='<p><span class="badge" style="background-color:green;font-family:arial;color:bule">协作信息</span></p>';
		                	}
		                	else if(chanceType=='2'){
		                		dOut ='<p><span class="badge" style="background-color:green;font-family:arial;color:bule">传递信息</span></p>';
		                	}
		                    return dOut;

		                }
		            },
		            {
		                data: 'saleChance.source'
		            },
		            {
		                data: 'saleChance.sourceUnit',
						render:function(data,type,full,meta){
							if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
							}
							//return "<div title='"+data+"' style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>"+data+"</div>"
						}
		                
		            },
		            {
		                data: 'saleChance.user.empLogin.userName'
		                
		            },
		            {
		                data: 'saleChance.business'
		            },
		            {
		            	data: 'saleChance.user.saleArea.superiorArea'
		            },
		            {
		                data: 'saleChance.sourceOfferInfo',
						render:function(data,type,full,meta){
							if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 20) ? '<font title="'+data+'">'+data.substring(0,20)+'...'+'</font>' : data;
							}
							//return "<div title='"+data+"' style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>"+data+"</div>"
						}
		                
		            },
		            {
		                data: 'saleChance.projectState',
						render:function(data,type,full,meta){
							if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 30) ? '<font title="'+data+'">'+data.substring(0,30)+'...'+'</font>' : data;
							}
							//return "<div title='"+data+"' style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>"+data+"</div>"
						}
		            },
		            {
		                data: 'saleChance.deviceState',
						render:function(data,type,full,meta){
							if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 20) ? '<font title="'+data+'">'+data.substring(0,20)+'...'+'</font>' : data;
							}
							//return "<div title='"+data+"' style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>"+data+"</div>"
						}
		            },
		            {
		                data: 'saleChance.sourcePower'
		            },
		            {
		                data: 'saleChance.orderAmount'
		                
		            },
		            {
		                data: 'saleChance.orderUnit',
						render:function(data,type,full,meta){
							if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 20) ? '<font title="'+data+'">'+data.substring(0,20)+'...'+'</font>' : data;
							}
							//return "<div title='"+data+"' style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>"+data+"</div>"
						}
		                
		            },
		            {
		                data: 'saleChance.orderPerson'
		                
		            },
		            {
		                data: 'saleChance.orderPersonPhone'
		                
		            },
		            {
		                data: 'saleChance.orderUnitAddr',
						render:function(data,type,full,meta){
							if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 20) ? '<font title="'+data+'">'+data.substring(0,20)+'...'+'</font>' : data;
							}
							//return "<div title='"+data+"' style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>"+data+"</div>"
						}
		                
		            },
		            {
		            	data: 'saleChance.createTime',
		            	render : function(data,type, full,meta) {
								if (data) 
								{
									return moment(data).format("YYYY-MM-DD");
								}
		            	}
		                
		            } 
		          ],
		        "drawCallback": function (oSettings) {
		            var k = 0;
		            var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

		            for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
		                $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
		                k++;
		            }

		            serverTable.off('click', 'tr');
		            serverTable.on('click', 'tr', function (event) {
		                event = event || window.event;
		                var target = event.target || event.scrElement;
		                var data = serverTable.row(this).data();

		                return;
		            });
		        }
		    });
		    //接收信息
		    jQuery('.receive-btn').on('click', function() {
		    	var selects=getSelect("#dataTable",1);
		    	var url = window.rootPath + "/ui/chanceReceive/receive";
		    	receiveSelect(selects,url,serverTable) 
		    });
	    	});
	  //查看详情
	    jQuery('.detail-btn').on('click', function() {
	    	var selects=getSelect("#dataTable",1);
	  	   if(selects.length>1){
	  		   $.springLemonDialog({
	  		         content: '只能操作一条记录',// 确认文本
	  		         clickButton: function (sender, modal, index) {// 按钮事件处理
	  		             $(this).closeDialog(modal);
	  		         }
	  		   }); 
	  	   }
	  	   else if(selects.length==0){
	  		   $.springLemonDialog({
	  		         content: '请选择要操作的记录',// 确认文本
	  		         clickButton: function (sender, modal, index) {// 按钮事件处理
	  		             $(this).closeDialog(modal);
	  		         }
	  		   }); 
	  	   }else if(selects.length==1){
	  		 var id=selects[0].saleChance.id;
	  		window.location = window.rootPath + "/ui/chance/detail"+"?chanceId="+id;  
	  	   }
		});
		  //查看详情2
	    jQuery('.detail-btn2').on('click', function() {
	    	var selects=getSelect("#dataTable2",1);
	  	   if(selects.length>1){
	  		   $.springLemonDialog({
	  		         content: '只能操作一条记录',// 确认文本
	  		         clickButton: function (sender, modal, index) {// 按钮事件处理
	  		             $(this).closeDialog(modal);
	  		         }
	  		   }); 
	  	   }
	  	   else if(selects.length==0){
	  		   $.springLemonDialog({
	  		         content: '请选择要操作的记录',// 确认文本
	  		         clickButton: function (sender, modal, index) {// 按钮事件处理
	  		             $(this).closeDialog(modal);
	  		         }
	  		   }); 
	  	   }else if(selects.length==1){
	  		 var id=selects[0].saleChance.id;
	  		window.location = window.rootPath + "/ui/chance/detail"+"?chanceId="+id;  
	  	   }
		});
	    //已接收信息列表
	    var serverData2 = {};
	    serverData2.key='';
	    serverData2.state = '';
	    var serverTable2;
	    jQuery(document).ready(function () {
	    	serverTable2 = jQuery('#dataTable2').DataTable({
		        "serverSide": true,
		        "ordering": false,
		        "ajax": {
		            "dataType": "json",
		            "type": "get",
		            "cache": false,
		            "url":window.rootPath+ '/ui/chanceReceive/getMyChance',
		            "data": function (d) {
		                return $.extend({}, d, serverData2);
		            }
		        },
		        "iDisplayLength": 5,
		        columns: [
		            {
		            data: null
		            },
		            {
		                data: null,
		                defaultContent: "",
		                render: function (data, type, full, meta) {
		                	var closeReason = data.saleChance.closeReason;
		                	var operate ='<input type="checkbox" class="checkchild1">';
		                    return operate;

		                }
		            },
		            {
		            	data: 'saleChance.name',
						render:function(data,type,full,meta){
							if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 20) ? '<font title="'+data+'">'+data.substring(0,20)+'...'+'</font>' : data;
							}
							//return "<div title='"+data+"' style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>"+data+"</div>"
						}
		            },
		            {
		            	data: 'saleChance.chancePriority'
		            },
		            {
		                data: 'saleChance.source'
		            },
		            {
		                data: 'saleChance.sourceUnit',
						render:function(data,type,full,meta){
							if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 20) ? '<font title="'+data+'">'+data.substring(0,20)+'...'+'</font>' : data;
							}
							//return "<div title='"+data+"' style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>"+data+"</div>"
						}
		                
		            },
		            {
		                data: 'saleChance.user.empLogin.userName'
		                
		            },
		            {
		            	data: 'saleChance.business'
		            		
		            },
		            {
		            	data: 'saleChance.user.saleArea.superiorArea'
		            },
		            {
		                data: 'saleChance.sourceOfferInfo',
						render:function(data,type,full,meta){
							if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 20) ? '<font title="'+data+'">'+data.substring(0,20)+'...'+'</font>' : data;
							}
							//return "<div title='"+data+"' style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>"+data+"</div>"
						}
		                
		            },
		            {
		                data: 'saleChance.projectState',
						render:function(data,type,full,meta){
							if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 20) ? '<font title="'+data+'">'+data.substring(0,20)+'...'+'</font>' : data;
							}
							//return "<div title='"+data+"' style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>"+data+"</div>"
						}
		            },
		            {
		                data: 'saleChance.deviceState',
						render:function(data,type,full,meta){
							if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 20) ? '<font title="'+data+'">'+data.substring(0,20)+'...'+'</font>' : data;
							}
							//return "<div title='"+data+"' style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>"+data+"</div>"
						}
		            },
		            {
		                data: 'saleChance.sourcePower'
		            },
		            {
		                data: 'saleChance.orderAmount'
		                
		            },
		            {
		                data: 'saleChance.orderUnit',
						render:function(data,type,full,meta){
							if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 20) ? '<font title="'+data+'">'+data.substring(0,20)+'...'+'</font>' : data;
							}
							//return "<div title='"+data+"' style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>"+data+"</div>"
						}
		                
		            },
		            {
		                data: 'saleChance.orderPerson'
		                
		            },
		            {
		                data: 'saleChance.orderPersonPhone'
		                
		            },
		            {
		                data: 'saleChance.orderUnitAddr',
						render:function(data,type,full,meta){
							if(data == null || data == undefined || data == ""){
								return data;
							}else{
								return (data.length > 20) ? '<font title="'+data+'">'+data.substring(0,20)+'...'+'</font>' : data;
							}
							//return "<div title='"+data+"' style='overflow:hidden; text-overflow:ellipsis; white-space:nowrap;'>"+data+"</div>"
						}
		                
		            },
		            {
		            	data: 'saleChance.createTime',
		            	render : function(data,type, full,meta) {
								if (data) 
								{
									return moment(data).format("YYYY-MM-DD");
								}
		            	}
		                
		            } 
		          ],
		        "drawCallback": function (oSettings) {
		            var k = 0;
		            var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

		            for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
		                $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
		                k++;
		            }

		            serverTable2.off('click', 'tr');
		            serverTable2.on('click', 'tr', function (event) {
		                event = event || window.event;
		                var target = event.target || event.scrElement;
		                var data = serverTable2.row(this).data();
		                return;
		            });
		        }
		    });
	    	});
	    function receiveSelect(selects,url,tableName){
	    	if(selects.length==0){
	    	       $.springLemonDialog({
	    	         content: '请选择要接收的信息',// 确认文本
	    	         clickButton: function (sender, modal, index) {// 按钮事件处理
	    	             $(this).closeDialog(modal);
	    	         }
	    	       }); 
	    	   }else{
	    		   var ids=selects[0].id;
	    		   for(var i=1;i<selects.length;i++){
	    			   ids += ',' + selects[i].id;  
	    		   }
	    		   var api=url+"?managerDeliveryIds="+ids;
	    		   $.springLemonDialog({
	    		         content: '确认接收'+selects.length+'条信息？',// 确认文本
	    		         clickButton: function (sender, modal, index) {// 按钮事件处理
	    		        	 if (index == 0) {
	    		        		 jQuery.ajax({
	    		        			    url:encodeURI(api),
	    		        	            cache:false,
	    		        	            type :"get",
	    		        	            dataType:"json",
	    		        	            contentType:"application/json;charset=utf-8",//解决返回数据中的中文乱码问题
	    		        	            success:function(data){
	    		        	            	tableName.draw(false);
	    		        	            	serverTable2.draw(false);
	    		        	            }
	    		        	        });	
	    		        	 }
	    		             $(this).closeDialog(modal);
	    		         }
	    		       });  
	    	   }
	    	
	    }
	    //查询
	    jQuery('.searchBtn').on('click', function() {
			serverData.key = jQuery('#keyword').val();
			serverTable.draw();
		});
		// 撤销查询
		jQuery('.cancelBtn').on("click", function() {
			jQuery('#keyword').val('');
			serverData.key = '';
			serverTable.draw();
		});
		   ///////列表中的全选
	    $("#checkAll").click(function(){
	    	var check = $(this).prop("checked");
	        $(".checkchild").prop("checked", check);
		});
	    $("#checkAll1").click(function(){
	    	var check = $(this).prop("checked");
	        $(".checkchild1").prop("checked", check);
		});
});